package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.OrderDetailAty
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.OrderListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.order_fgm.view.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\5\20 0020.
 */
class OrderFgm :Fragment(), SpringView.OnFreshListener {
    companion object {
        fun getInstence(type:String):OrderFgm{
            val allGoodsFgm = OrderFgm()
            val bundle = Bundle()
            bundle.putString("type", type)
            allGoodsFgm.arguments = bundle
            return allGoodsFgm
        }
    }
    private lateinit var rootView:View
    private var status=""
    private val datas= ArrayList<OrderListBean.DataBean>()
    private lateinit var adp: BaseQuickAdapter<OrderListBean.DataBean, BaseViewHolder>
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater?.inflate(R.layout.order_fgm,null)!!

        rootView.spView.header=DefaultHeader(context)
        rootView.spView.setListener(this)
        status = when(arguments.get("type") as String){
            "1"->""
            "2"->"1"
            "3"->"2"
            "4"->"3"
            else->""
        }

        initRecy()
        getData()
        return rootView

    }

    private fun initRecy() {
        adp=object :BaseQuickAdapter<OrderListBean.DataBean,BaseViewHolder>(R.layout.order_item,datas){
            override fun convert(helper: BaseViewHolder?, item: OrderListBean.DataBean?) {
                Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                helper?.setText(R.id.itemTittle,item?.title)
                if (item?.money?.equals("0")!!){
                    helper?.setText(R.id.itemPrice,"${item?.score}积分")
                }else{
                    helper?.setText(R.id.itemPrice,"￥${item?.money}")
                }
                helper?.setText(R.id.itemTime, MyUtils.timeStampToStrYMD(item?.create_time?.toLong()!!))
                if (item?.type.equals("1")){
                    when(item?.shoping_status){
                        0->{
                            helper?.setText(R.id.itemStatus,"未支付")
                        }
                        10->{
                            helper?.setText(R.id.itemStatus,"配货中")

                        }
                        20->{
                            helper?.setText(R.id.itemStatus,"已发货")

                        }
                        30->{
                            helper?.setText(R.id.itemStatus,"已收货")
                        }
                    }
                }else if (item?.type.equals("6")){
                    helper?.setText(R.id.itemStatus,"")
                }


                val btDelet = helper?.getView<TextView>(R.id.btDelet)
                if (item?.type.equals("1")){
                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                    btDelet?.visibility=View.VISIBLE
                }else{
                    Glide.with(mContext).load(R.mipmap.logo).asBitmap().into(helper?.getView(R.id.itemIcon))
                    btDelet?.visibility=View.GONE
                }
                btDelet?.setOnClickListener {
                    AlertDialog.Builder(context)
                            .setTitle("确认删除该订单？")
                            .setPositiveButton("确定"){
                                dialog, which ->
                                OkGo.post<BaseBean>(Urls.BaseUrl+ Urls.DELET_ORDER)
                                        .params("id",item?.id.toString())
                                        .execute(object : JsonCallBack<BaseBean>(activity){
                                            override fun onSuccess(response: Response<BaseBean>?) {
                                                val bean = response?.body()
                                                if (bean?.code==1){
                                                    MyUtils.toast("删除成功")
                                                    datas.clear()
                                                    this@OrderFgm.getData()
                                                }else{
                                                    MyUtils.toast(bean?.msg)
                                                }
                                                dialog.dismiss()
                                            }
                                        })
                            }
                            .setNegativeButton("取消"){
                                dialog, which ->
                                dialog.dismiss()
                            }.show()
                }
            }
        }

        rootView.recyclerview.layoutManager= LinearLayoutManager(context, LinearLayout.VERTICAL,false)
        rootView.recyclerview.adapter=adp
        adp.setOnItemClickListener { adapter, view, position ->
            if (datas[position].type.equals("1")){
                val intent = Intent(context, OrderDetailAty::class.java)
                intent.putExtra("oid",datas[position].id)
                startActivity(intent)
            }else{

            }

        }
    }
    private fun getData() {
        OkGo.post<OrderListBean>(Urls.BaseUrl+Urls.ORDER_LIST)
                .params("status",status)
                .execute(object :JsonCallBack<OrderListBean>(activity){
                    override fun onSuccess(response: Response<OrderListBean>?) {
                        rootView.spView.onFinishFreshAndLoad()
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data!=null){
                                datas.addAll(bean?.data)
                                adp.notifyDataSetChanged()
                            }
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
    }
    override fun onLoadmore() {
    }

    override fun onRefresh() {
        datas.clear()
        getData()
    }
}